package com.zut.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zut.mapper.BookorderMapper;
import com.zut.mapper.ParentsMapper;
import com.zut.mapper.SchduleMapper;
import com.zut.mapper.TeacherMapper;
import com.zut.pojo.*;
import com.zut.service.BookorderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bookorder
 * @since 2021-06-06
 */
@Service
public class BookorderServiceImpl extends ServiceImpl<BookorderMapper, Bookorder> implements BookorderService {

    @Resource
    private BookorderMapper bookorderMapper;
    @Resource
    private ParentsMapper parentsMapper;
    @Resource
    private SchduleMapper schduleMapper;
    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private BookorderService bookorderService;

    //预约课程
    @Override
    public int bookClass(Integer parid, Integer teaid, Integer week, Integer day, Date ordertime) {
        QueryWrapper<Schdule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teaid",teaid)
                .eq("week",week)
                .eq("day",day);
        //更新选课表状态
        Schdule schdule = schduleMapper.selectOne(queryWrapper);
        //判断老师是否可选
        if(schdule.getState1()==0)
            return 0;
        schdule.setParid(parid);
        schdule.setState2(1);
        Parents parents=parentsMapper.selectById(parid);
        //判断家长课时是否充足
        if(parents.getParhour()<=0)
            return 0;
        Date date=new Date();
        //判断选课时间是否在当前时间之后
        if(ordertime.compareTo(date)==-1)
            return 0;
        //家长课时减一
        parents.setParhour(parents.getParhour()-1);
        //老师课程加一
        Teacher teacher = teacherMapper.selectById(teaid);
        teacher.setTeahour(teacher.getTeahour()+1);
        //增加预约课程订单信息
        Bookorder bookorder=new Bookorder();
        bookorder.setParid(parid);
        bookorder.setTeaid(teaid);
        int res=bookorderMapper.insert(bookorder);
        return res;
    }

    //查看已预约课程
    @Override
    public List<Bookorder> selectAllBookClass(Integer parid) {
        List<Bookorder> bookorders = bookorderService.selectAllBookClass(parid);
        return bookorders;
    }
}
